package com.yun.fullstack.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.core.util.StrUtil;
import com.yun.fullstack.common.Result;
import com.yun.fullstack.entity.Message;
import com.yun.fullstack.mapper.MessageMapper;
import com.yun.fullstack.service.MessageService;
import com.yun.fullstack.util.IpAddressUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.rsocket.server.ConfigurableRSocketServerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 祝福信息表 前端控制器
 * </p>
 *
 * @author XiaoWenzhuo
 * @since 2022-04-26
 */
@RestController
@Validated
@Slf4j
@RequestMapping("/message")
@Api(value = "祝福信息表控制器", tags = {"祝福信息管理"})
public class MessageController {
    @Autowired
    private MessageService messageService;
    @Autowired
    private HttpServletRequest request;

    //@ApiOperation(value = "获取全部祝福信息", notes = "获取全部祝福信息")
    //@GetMapping("/getAll")
    //public Result<List<Message>> getAll(){
    //    return Result.success(messageService.list());
    //}

    @ApiOperation(value ="插入祝福信息", notes = "插入祝福信息")
    @PostMapping("/insert")
    public Result<?> insert(@RequestBody @Validated Message message){
        String ipAddress = IpAddressUtils.getIpAddress(request);
        //log.info("ipAddress:{}",ipAddress);
        String place = IpAddressUtils.getCityInfo(ipAddress);
        //默认头像
        log.info("messageAvatar:{}",message.getAvatar());
        if(message.getAvatar().isBlank()){
            message.setAvatar("https://tyut-calendar.oss-accelerate.aliyuncs.com/messageBoard/2022-05-0/0f7282d0-3881-47e7-9ce1-794c8961201cQQ图片20220505113206.jpg");
        }
        //log.info("place:{}",place);
        message.setPlace(place);
        message.setCreateTime(LocalDateTime.now());
        messageService.save(message);
        return Result.success(message);
    }

    //@ApiOperation(value = "删除祝福信息", notes = "删除祝福信息")
    //@DeleteMapping("/delete/{id}")
    //public Result<?> delete(@PathVariable("id") Integer id){
    //    return Result.success(messageService.removeById(id));
    //}
    //
    //@ApiOperation(value = "更新祝福信息", notes = "更新祝福信息")
    //@PutMapping("/update")
    //public Result<?> update(@RequestBody @Valid Message message){
    //    message.setCreateTime(LocalDateTime.now());
    //    return Result.success(messageService.updateById(message));
    //}

    @ApiOperation(value = "分页查找祝福信息", notes = "分页查找祝福信息")
    @GetMapping("/findPage")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize
                              ) {
        LambdaQueryWrapper<Message> wrapper = Wrappers.<Message>lambdaQuery();
        Page<Message> userPage = messageService.page(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(userPage);
    }

}
